<template>
  <!--  车间日报表-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" :rules="formRules" ref="searchForm" class="demo-form-inline">
        <el-form-item label="退回单号" prop="rejectCode" style="width: 250px">
          <el-input v-model="searchParam.rejectCode" placeholder="请输入退回单号" clearable style="width: 160px"/>
        </el-form-item>
        <el-form-item label="批次号" prop="materialBatchNo" style="width: 300px">
          <el-input v-model="searchParam.materialBatchNo" placeholder="请输入批次号" clearable style="width: 300px"/>
        </el-form-item>
        <!--				<el-form-item label="工厂/车间" prop="organizationId" style="width: 250px">-->
        <!--					<el-tree-select v-model="searchParam.organizationId" @change="handleOrganizationChange" :data="workRoomList"-->
        <!--													check-strictly :props="{ children: 'sonList', label: 'orgName', value: 'id' }" />-->
        <!--				</el-form-item>-->
        <!--				<el-form-item label="设备" prop="equipmentIds" style="width: 250px">-->
        <!--					<el-select v-model="searchParam.equipmentIds" placeholder="请选择设备" clearable filterable multiple>-->
        <!--						<el-option v-for="item in equipmentData" :key="item.id" :label="item.equipmentName" :value="item.id" />-->
        <!--					</el-select>-->
        <!--				</el-form-item>-->

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="submitForm()">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
        <el-table-column label="序号" align="center" width="70">
          <template #default="scope"> {{ (pageSearch.current - 1) * pageSearch.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="rejectCode" label="退回单号" align="center" min-width="200"/>
        <el-table-column prop="materialName" label="物料名称" align="center" min-width="200"/>
        <el-table-column prop="materialNo" label="物料编码" align="center" min-width="200"/>
        <el-table-column prop="materialBatchNo" label="批次号" align="center" min-width="250"/>
        <el-table-column prop="traceabilityCodePlace" label="模具号" align="center" min-width="200"/>
        <el-table-column prop="traceabilityCode" label="追溯码" align="center" min-width="280" show-overflow-tooltip/>
        <el-table-column prop="outsourcingNo" label="委外单号" align="center" min-width="200"/>
        <el-table-column prop="procedureOrderNo" label="工序订单号" align="center" min-width="200"/>
        <el-table-column prop="rejectCount" label="退回数量" align="center" min-width="200"/>
        <el-table-column prop="rejectCause" label="退回原因" align="center" min-width="200"/>
        <el-table-column prop="createTime" label="创建时间" align="center" min-width="200"/>
        <el-table-column prop="createUserName" label="创建人" align="center" min-width="200"/>
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
                       :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
                       :total="pageTotal"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {ref, onMounted} from 'vue';
import {ElMessage, ElForm} from 'element-plus';
import axiosInstance from '@/utils/http/api';
import {Delete, Edit, Plus, Refresh, Search} from '@element-plus/icons-vue';
import dayjs from 'dayjs';

onMounted(() => {
  getList();
  getOption();
});

const searchForm = ref<InstanceType<typeof ElForm>>();

const pageSearch = ref({
  current: 1,
  pageSize: 10
});

const pageTotal = ref(null); // 页面总数
const searchParam = ref({});

const paginatedData = ref([]);
const workRoomList = ref([]); // 车间
const equipmentData = ref([]);

const formRules = ref({
  organizationId: [{required: true, message: '请选择工厂/车间', trigger: 'change'}],
  equipmentIds: [{required: true, message: '请选择设备', trigger: 'change'}]
});

const handleOrganizationChange = (value) => {
  axiosInstance.post('/api/basedataEquipment/getAll', {organizationId: value}).then(res => {
    if (res.code !== 200) {
      ElMessage.error(res.message);
      return false;
    }
    equipmentData.value = res.data;
  });
};

const getOption = () => {
  axiosInstance.post('/api/basedataOrganization/getAll', {})
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        workRoomList.value = res.data;
      });
};
const materialProperData = ref([
  {name: '采购件', id: '0'},
  {name: '半成品', id: '1'},
  {name: '成品件', id: '2'},
  {name: '模具', id: '3'},
  {name: '刀具', id: '4'},
  {name: '备件', id: '5'},
  {name: '量具', id: '6'},
  {name: '虚拟件', id: '7'}
]);
// 获取列表接口
const getList = () => {

  axiosInstance.post('/api/outsourcingWorkMaterialRejectCode/getPage', {
    ...searchParam.value,

    ...pageSearch.value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        paginatedData.value = res.data.records;
        pageTotal.value = res.data.total;
      });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 提交表单
const submitForm = () => {
  searchForm.value.validate((valid) => {
    if (valid) {
      getList();
    } else {
      ElMessage.error('请检查表单输入');
      return false;
    }
  });
};

// 重置搜索条件
const resetSearch = () => {
  pageSearch.value.current = 1;
  searchForm.value.clearValidate();
  searchParam.value = {};
  getList()
  // submitForm()
};
</script>
<style scoped></style>